package by.bsu.touragency.db.beans;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Person {

	private int id;
	private boolean isAdmin;
	private String login;
	private Long password;
	private String email;

	public Person() {
	}

	public Person(String login, String password) {
		if (validityCheckLogin(login))
			this.login = login;
		if (validityCheckPassword(password))
			this.password = (long) password.hashCode();

	}

	public Person(String login, String password, String email) {
		if (validityCheckLogin(login))
			this.login = login;
		if (validityCheckPassword(password))
			this.password = (long) password.hashCode();
		if (validityCheckEmail(email))
			this.email = email;

	}

	public Person(String login, String password, String email, boolean b)
			throws Exception {
		if (validityCheckLogin(login))
			this.login = login;
		if (validityCheckPassword(password))
			this.password = (long) password.hashCode();
		if (validityCheckEmail(email))
			this.email = email;
		this.isAdmin = b;
	}

	public void setId(int id) {
		this.id = id;
	}

	public Long getPassword() {
		return password;
	}

	public int getId() {
		return id;
	}

	public String getLogin() {
		return login;
	}

	public void setPassword(String password) {
		this.password = (long) password.hashCode();
	}

	public void setPassword(Long password) {
		this.password = password;

	}

	public void setLogin(String login) {
		this.login = login;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public boolean isAdmin() {
		return isAdmin;
	}

	public void setAdmin(boolean isAdmin) {
		this.isAdmin = isAdmin;
	}

	public boolean validityCheckLogin(String login) {
		if (login.length() < 50) {
			Pattern p = Pattern.compile("\\w*");
			Matcher m = p.matcher(login);
			return m.matches();
		}
		return false;
	}

	public boolean validityCheckPassword(String password) {
		if (password.length() < 20) {
			Pattern p = Pattern
					.compile("(?!^[0-9]*$)(?!^[a-zA-Z]*$)^([a-zA-Z0-9]{6,20})$");
			Matcher m = p.matcher(password);
			return m.matches();

		}
		return false;
	}

	public boolean validityCheckEmail(String email) {
		if (email.length() < 50) {
			Pattern p = Pattern
					.compile("^([0-9a-zA-Z]([-.\\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\\w]*[0-9a-zA-Z]\\.)+[a-zA-Z]{2,9})$");
			Matcher m = p.matcher(email);
			return m.matches();
		}
		return false;
	}
}
